/*
 * Developer : Jack Matthews
 * Email: developer@droidprofessor.com
 * 
 * Copyright (C) 2010 http://droidprofessor.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.droidprofessor.android.library.phonelicenses;

import java.io.BufferedInputStream;
import java.io.IOException;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

class Utils {
	private static final String TAG = "Utils";
	private Utils(){};
	
//	/**
//	 * An error alert dialog
//	 */
//	static void showDialogError(Context cnx) {
//		new AlertDialog.Builder(cnx)
//		.setIcon(android.R.drawable.ic_dialog_alert)
//		.setTitle( cnx.getResources().getString(R.string.com_phonelicenses_error) )
//		.setMessage(R.string.com_phonelicenses_communication_error)
//		.setPositiveButton(R.string.com_phonelicenses_ok, null)
//		.show();
//	}
//	
//	
//	/**
//	 * An error alert dialog
//	 */
//	static void showDialogMessage(Context cnx, CharSequence message) {
//		new AlertDialog.Builder(cnx)
//		.setIcon(android.R.drawable.ic_dialog_alert)
//		//.setTitle( cnx.getResources().getString(R.string.com_phonelicenses_error) )
//		.setMessage(message)
//		.setPositiveButton(R.string.com_phonelicenses_ok, null)
//		.show();
//	}
//	
//	
//	/**
//	 * An error alert dialog
//	 */
//	static void showDialogMessage(Context cnx, int message) {
//		new AlertDialog.Builder(cnx)
//		.setIcon(android.R.drawable.ic_dialog_alert)
//		//.setTitle( cnx.getResources().getString(R.string.com_phonelicenses_error) )
//		.setMessage(cnx.getString(message))
//		.setPositiveButton(R.string.com_phonelicenses_ok, null)
//		.show();
//	}
	
	
	
	/**
	 * Log data coming from an InputStream
	 * Debugging purposes only
	 * 
	 * @param bis the InpuStream
	 */
	static void logInpuStream(BufferedInputStream bis) {
		//Print the result to the screen for debugging puroposes
		{
			try {
				Log.d(TAG, "################ START INPUT STREAM");
				bis.mark(1024 * 1024);
				
				int i;
				StringBuilder b = new StringBuilder();
				while( (i=bis.read()) != -1 ) {
					
					b.append((char)i);
				}
				
				Log.d(TAG, b.toString());
				Log.d(TAG, "################ END INPUT STREAM");
				
				bis.reset();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	

	
	
	
	
	/**
	 * Is there an Internet Connection?
	 * 
	 * @return boolean true if there is an internet connection, false otherwise
	 */
	static boolean gotInternetConnection(Context cnx) {
		ConnectivityManager connectivity = (ConnectivityManager) cnx.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo[] networks = connectivity.getAllNetworkInfo();
		
		//Check that at least one connection is available
		for(NetworkInfo network : networks) {
			if(network.isConnected())
				return true;
		}
		
		return false;
	}

}
